<?php
namespace app\service;

use app\common\basics\Logic;
use app\common\model\wechat\Wechat;
use app\common\model\wechat\WechatReply;
use app\common\server\WeChatServer;
use EasyWeChat\Kernel\Exceptions\Exception;
use EasyWeChat\Kernel\Messages\Text;
use EasyWeChat\Factory;
use think\Log;

class WechatService 
{

    /** 
   * 初始化微信账户
   */
   public static function initAccount($mode = 1)
   {
        $config = config('wechat');//获取配置参数
        /* $config = [
            'app_id' => 'wx3cf0f39249eb0xxx',
            'secret' => 'f1c242f4f28f735d4687abb469072xxx',
            'token' => 'TestToken',
            'response_type' => 'array',
            //...
        ]; */
        switch($mode){
            case 1: 
               // 公众号
               $app = Factory::officialAccount($config);
               break;
            case 2: 
                 // 小程序
                 $app = Factory::miniProgram($config);
                break;  
            case 3: 
                // 开放平台
                $app = Factory::openPlatform($config);
                break; 
            case 4: 
                // 企业微信开放平台
                $app = Factory::openWork($config);
                break;  
            case 5: 
                // 微信支付
                $app = Factory::payment($config);
                break; 
            default:    
                // 公众号
               $app = Factory::officialAccount($config);
               break;         
       }
       return $app;

   }

    /**
     * 获取微信配置
     * @param $url
     * @return array|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public static function jsConfig($url)
    {
        $config = config('wechat');//获取配置参数
        $app = Factory::officialAccount($config);
        $url = urldecode($url);
        $app->jssdk->setUrl($url);
        //$apis = ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone', 'openLocation', 'getLocation', 'chooseWXPay', 'updateAppMessageShareData', 'updateTimelineShareData', 'openAddress'];
        $apis = ['updateTimelineShareData','updateAppMessageShareData'];try {
            return  $app->jssdk->getConfigArray($apis, $debug = false, $beta = false);
        } catch (Exception $e) {
            Log::error($e->getMessage());
            return false;
        }
    }
}